package bobo;

import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.*;
import com.github.javaparser.resolution.types.ResolvedType;
import com.github.javaparser.symbolsolver.JavaSymbolSolver;
import com.github.javaparser.symbolsolver.resolution.typesolvers.*;
import com.github.javaparser.ast.Node;
import com.github.javaparser.printer.*;
import com.github.javaparser.printer.XmlPrinter;
import com.github.javaparser.ast.expr.BinaryExpr;
import com.github.javaparser.ast.stmt.IfStmt;
import com.github.javaparser.ast.stmt.Statement;
import com.github.javaparser.ast.visitor.ModifierVisitor;
import com.github.javaparser.ast.visitor.Visitable;
import com.github.javaparser.utils.CodeGenerationUtils;
import com.github.javaparser.utils.Log;
import com.github.javaparser.utils.SourceRoot;

import java.io.*;
import java.io.FileNotFoundException;

/**
 * Hello world!
 *
 */
public class App
{
    private static final String FILE_PATH="~/work/javaparser/ast/ast/src/main/java/bobo/app/App.java";
    public static void main( String[] args )
    {
        if (args.length <2) {
            System.err.println("parameter error!");
            System.exit(-1) ;
        }

        String str_input_file = args[0];
        String str_out_file   = args[1];

        CombinedTypeSolver combindTypeSolver = new CombinedTypeSolver();
        combindTypeSolver.add(new ReflectionTypeSolver());

        JavaSymbolSolver symbolSolver = new JavaSymbolSolver(combindTypeSolver);


        JavaParser parser = new JavaParser();
        parser.getParserConfiguration().setSymbolResolver(symbolSolver);
        try {
            ParseResult<CompilationUnit> result = parser.parse(new File(str_input_file));

            if (!result.isSuccessful()) {
                System.err.println("Parse file error!");
                System.exit(-1) ;
            }

            CompilationUnit cu = result.getResult().get();


            //YamlPrinter printer = new YamlPrinter(true);
            //System.out.println(printer.output(compilationUnit));

            MyXmlPrinter printer2 = new MyXmlPrinter(true);
            String str_ast = printer2.output(cu);

            PrintWriter out = new PrintWriter(str_out_file);
            out.println(str_ast);
            out.close();
            //System.out.println(str_ast);

            System.exit(0) ;
        } catch (FileNotFoundException e) {
            e.printStackTrace();
            System.err.println("File not found!");
            System.exit(-1) ;
        }
    }
}